﻿package rd.hardcore
{
	import caurina.transitions.Tweener;
	import flash.desktop.NativeApplication;
	import flash.desktop.SystemTrayIcon;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.NativeWindow;
	import flash.display.NativeWindowInitOptions;
	import flash.display.Screen;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.SharedObject;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	import rd.hardcore.effects.WishEffects;
	import rd.hardcore.engine.DragDropEvent;
	import rd.hardcore.engine.DragDropManager;
	import rd.hardcore.engine.SaveData;
	import rd.hardcore.selector.Selector
	import rd.hardcore.selector.Selector_FC;
	
	/**
	 * ...
	 * @author Romain Prache
	 */
	public class Main extends Sprite 
	{
		
		public var conteneurSon		: Sound;
		public var canal			: SoundChannel;
		public var volumeManager	: SoundTransform;
		public var globalDatas		: Datas;
				
		private var container		: Sprite;
		
		static public var song		: String = "medias/sound/Hardcore.mp3";
		private var selector		: Selector;
		private var loader			: Loader;
		private var fx				: WishEffects;
		private var window			: NativeWindow;
		private var myTimer			: Timer;
		private var isDraging		: Boolean;
		private var first			: Boolean = true;
		
		public function Main():void 
		{
			trace("HARDCORE");
			
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event= null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			placeWindow();
			registerStartUp();
			//putIcon();
			
			globalDatas = new Datas();
			globalDatas.addEventListener(Datas.DATAS_READY, display);
			
			container = new Sprite();
			addChild(container);
			
			
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, failLoadBG);
			container.addChild(loader);
			
			canal = new SoundChannel();
			conteneurSon = new Sound();
			
			conteneurSon.load(new URLRequest(song));
			
			/*
			var zone : Sprite = new Sprite();
			addChild(zone);
			
			zone.graphics.beginFill(0, 0);
			zone.graphics.drawRect(0, 0, 200, 200);
			zone.graphics.endFill();*/
			
			var dropZone:DragDropManager = new DragDropManager();
			dropZone.addEventListener(DragDropEvent.DRAGDROP_FAIL, dragNdropFail);
			dropZone.addEventListener(DragDropEvent.DRAGDROP_SUCCEED, refreshContent);
			addChild(dropZone);
			
			dropZone.addEventListener(MouseEvent.MOUSE_DOWN, 	startDraging);
			dropZone.addEventListener(MouseEvent.MOUSE_UP, 		handleClick);
			dropZone.buttonMode = true;
			
			var sysIcon:HardIcon = new HardIcon();
			//var menu:HardMenu = new HardMenu(this);
			
			window = new NativeWindow(new NativeWindowInitOptions());
			window.alwaysInFront = true;
			window.orderToFront();
		}
		
		
		/**
		 * Initialise le lancement à l'ouverture de la session lors de la 1ère utilisation
		 */
		private function registerStartUp():void
		{
			var so:SharedObject = SharedObject.getLocal("hardcoreApp");
			if (!so.data.firstRun)
			{
				so.data.firstRun = true;
				try {
					NativeApplication.nativeApplication.startAtLogin = true;
				}catch (err:Error)
				{
					//
				}
			}
		}
		
		private function placeWindow():void 
		{
			var bounds:Rectangle = Screen.mainScreen.visibleBounds;
			stage.nativeWindow.x = bounds.left + (bounds.width - stage.stageWidth);
			stage.nativeWindow.y = bounds.top + (bounds.height - stage.stageHeight) / 2;
			
		}
	
		
		private function display(e:Event):void 
		{
			globalDatas.removeEventListener(Datas.DATAS_READY, display);
			
			if (first)	loader.load(new URLRequest( Datas.datas.bg.toString() ));
			first = false;
			
			selector = new Selector_FC();
			addChild(selector);
			selector.y = 200;
			selector.alpha = 0;
			Tweener.addTween(selector, {alpha:1, time:.5 } );
			
			fx = new WishEffects();
			addChild(fx);
		}
		
		private function failLoadBG(e:IOErrorEvent):void 
		{
			trace("fail : load bg");
		}
		
		private function startDraging(e:MouseEvent):void 
		{
			stage.nativeWindow.startMove();
			// Check s'il s'agit d'un click ou d'un drag
			isDraging = false;
			myTimer = new Timer(150,1);
			myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
			myTimer.start();
			trace("startDraging");
		}
		
		private function timerComplete(e:TimerEvent):void 
		{
			trace("timerComplete");
			myTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
			isDraging = true;
			myTimer.stop();
		}
		
		/* Click */
		private function handleClick(e:MouseEvent):void 
		{
			trace("2: CLICK");
			if (!isDraging)
			{
				trace("2: not draging");
				trace(song);
				conteneurSon = new Sound();
				conteneurSon.load(new URLRequest(song));
				
				fx.feuerFrei();
				//var writer:SaveData = new SaveData();
				
				canal = conteneurSon.play();
			}
		}
		
		
		private function refreshContent(e:DragDropEvent):void 
		{
			Tweener.addTween(selector, {alpha:0, time:.5, onComplete:reloadXML } );
		}
		
		private function reloadXML():void
		{
			removeChild(selector);
			globalDatas = new Datas();
			globalDatas.addEventListener(Datas.DATAS_READY, display);
		}
		
		
		/**
		 * Utilisé en cas d'échec du drag and drop de fichiers
		 */
		private function dragNdropFail(e:DragDropEvent):void {
			conteneurSon = new Sound();
			conteneurSon.load(new URLRequest("medias/sound/fail.mp3"));
			
			canal = conteneurSon.play();
		}
		
	}
	
}